<template>
  <div class="pf-bgwhite pf-p-10">
    <div class="pf-mt-20 pf-text-left">
      <el-button icon="Refresh" @click="initDatas"></el-button>
      <el-button type="primary" @click="createGuest">新建</el-button>
      <el-button @click="deleteGuest">删除</el-button>
    </div>
    <pf-table
      class="pf-mt-20"
      :config="tableConfig"
      :data="tableData"
      :page-param="pageParam"
      @page-size-change="pageSizeChange"
      @page-change="pageChange"
      @current-page-change="pageChange"
    >
      <el-table-column label="操作" fixed="right" width="80">
        <template #default="scope">
          <el-tooltip effect="light" placement="left" trigger="hover">
            <component :is="'Menu'" class="el-button--text" style="width: 18px"></component>
            <template #content>
              <div class="pf-flex pf-flex-column pf-flex-0">
                <el-button @click="handleClick(scope.row, 'edit')" link type="primary" size="mini"
                  >编辑</el-button
                >
                <el-button @click="handleClick(scope.row, 'xxx')" link type="primary" size="mini"
                  >员工关联</el-button
                >
                <el-button @click="handleClick(scope.row, 'user')" link type="primary" size="mini"
                  >员工查看</el-button
                >
              </div>
            </template>
          </el-tooltip>
        </template>
      </el-table-column>
    </pf-table>
    <el-dialog title="用户列表" :width="'80%'" v-model:model-value="userDialogVis">
      <pf-table class="pf-mt-20" :config="userTableConfig" :data="userTableData"> </pf-table>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, ref, Ref } from 'vue';
import { useNotice } from '@/components/element-plus/notice';
import { useData, emptyTable, useHttpClient } from 'pf-component';
import { TableNameEnum } from '@/constants/enum/table-name.enum';
import { useGuest } from '@/views/system/guest/use-guest';
import { systemApi } from '@/constants/api/system-api';

export default defineComponent({
  name: 'Guest',
  setup() {
    const userDialogVis = ref(false);
    const pageParam: any = reactive({ total: 0, pageSize: 10, pageNum: 1 });
    const tableConfig = ref(emptyTable);
    const tableData: Ref<any[]> = ref([]);

    const userTableConfig = ref(emptyTable);
    const userTableData: Ref<any[]> = ref([]);
    onMounted(() => {
      Promise.all([
        useData().loadTable([
          { name: TableNameEnum.guest, config: tableConfig },
          { name: TableNameEnum.sysUser, config: userTableConfig }
        ])
      ]).then(ress => {
        if (ress.findIndex(res => !res) === -1) {
          userTableConfig.value.showPage = false;
          initDatas();
        }
      });
    });
    const initDatas = () => {
      useGuest()
        .pageGuest({ ...pageParam })
        .then((res: any) => {
          tableData.value = res.records;
          pageParam.total = res.total;
        });
    };
    initDatas();
    const pageChange = (val: number) => {
      pageParam.pageNum = val;
      initDatas();
    };
    const pageSizeChange = (val: number) => {
      pageParam.pageSize = val;
      initDatas();
    };
    const createGuest = () => {
      useNotice().message.error('待完善');
      // TODO wangjie
    };
    const deleteGuest = () => {
      useNotice().message.error('待完善');
    };
    const openUserDialog = (userId: string) => {
      userDialogVis.value = true;
      initUserTableData(userId);
    };
    const initUserTableData = (userId: string) => {
      useHttpClient()
        .general(systemApi.userApi.listByGuest, undefined, { userId })
        .then(res => {
          userTableData.value = res.data;
        });
    };
    const handleClick = (data: any, cmd: string) => {
      switch (cmd) {
        case 'user':
          openUserDialog(data.userId);
          break;
        case 'edit':
          break;
        default:
      }
    };
    const formRefChange = (newV: any, prop: string) => {};

    return {
      userDialogVis,
      pageParam,
      pageChange,
      pageSizeChange,
      formRefChange,
      deleteGuest,
      createGuest,
      initDatas,
      tableConfig,
      tableData,
      userTableData,
      userTableConfig,
      handleClick
    };
  }
});
</script>

<style scoped lang="scss"></style>
